//
//  VisitorLoginView.swift
//  ZTFWeibo
//
//  Created by Apple on 16/2/3.
//  Copyright © 2016年 ztf. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate: NSObjectProtocol{
    //协议方法
    //登陆方法
    func visitorLogin()
    //注册
    func visitorRegister()
}


class VisitorLoginView: UIView {
    
    //声明代理属性
    //属性默认是强引用 需要添加weak
    weak var visitorDelegate: VisitorLoginViewDelegate?
    
    
    @objc func loginDidClick(){
        visitorDelegate?.visitorLogin()
    }
    @objc func registerDidClick(){
        visitorDelegate?.visitorRegister()
    }
    
    //重写构造方法
    init(){
        super.init(frame: CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:设置页面信心
    func setUIInfo(imageName: String, title: String, circleHidden: Bool) {
        iconView.image = UIImage(named: imageName)
        tipLabel.text = title
        circleView.hidden = circleHidden
        startAnimation()
        
    }
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.duration = 10
        //当动画结束或者视图处于非活跃状态 动画不移除图层
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    
    //MARK:访客视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //设置布局s
        //VFL   "view1.attr1 = view2.attr2 * multiplier + constant"
        //环绕约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //房子约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
//        //背景约束
//        addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //文案约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        //宽度约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //登陆按钮约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Left, relatedBy: .Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //注册按钮约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Right, relatedBy: .Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //背景约束
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView,"registerBtn":registerBtn]))
        
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerDidClick", forControlEvents: .TouchUpInside)
    }
    
    //MARK:懒加载所有的控件
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //提示文案
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.text = "不要因为爱人的沉默和不解风情而郁闷，因为时间会告诉你，越是平凡的陪伴，就越长久。"
        l.textAlignment = NSTextAlignment.Center
        l.font = UIFont.systemFontOfSize(14)
        l.numberOfLines = 0
        l.textColor = UIColor.lightGrayColor()
        l.sizeToFit()
        return l
    }()
    
    //按钮
    private lazy var loginBtn :UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        return btn
    }()
    
    private lazy var registerBtn :UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return btn
        }()
    
    
}
